package de.s9mtdev.lol_destiny;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class RiftView extends ImageView implements OnTouchListener {
	
	InSession insession;
	Bitmap bitmap;
	Paint paint = new Paint();
	Paint wardpaint = new Paint();
	Paint timepaint = new Paint();
	List<Ward> wards = new ArrayList<Ward>();
	Queue<Ward> dump = new ArrayBlockingQueue<Ward>(6);
	Ward undoable;
	float downXValue;
	float downYValue;
	
	public RiftView(Context context) {
		super(context);
		init();
	}
	
	public RiftView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public RiftView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	private void init() {
		setOnTouchListener(this);
		Resources res = getResources();
		insession = (InSession)getContext();
		bitmap = BitmapFactory.decodeResource(res, R.drawable.map_bmp);
		wardpaint.setColor(Color.RED);
        wardpaint.setAntiAlias(true);
        timepaint.setColor(Color.WHITE);
        timepaint.setStyle(Paint.Style.FILL);
        timepaint.setAntiAlias(true);
        timepaint.setTextSize(10);	
	}

	public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                downXValue = event.getX();
                downYValue = event.getY();
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                float currentX = event.getX();
                float currentY = event.getY();
                if (downXValue != currentX || downYValue != currentY)
                {
                	for (Ward w : wards) {
                		if (Math.min(currentX, downXValue) < w.x && Math.max(currentX, downXValue) > w.x
                				&& Math.min(currentY, downYValue) < w.y && Math.max(currentY, downYValue) > w.y) {
                			dump.add(w);
                			insession.sendMsg("say", "unward " + w.x + " " + w.y);
                		}
                	}                    
                }
                else
                {
                	int x = (int)event.getX();
                    int y = (int)event.getY();
                    undoable = new Ward(x, y);
                    setWard(undoable);
                    insession.sendMsg("say", "ward " + x + " " + y);  
                }                	
                break;
            }
        }
		return true;
	}
	
	@Override
	protected void onDraw(Canvas c){
		c.drawBitmap(bitmap,  0, 0, paint);
		Long now = System.currentTimeMillis();
        for ( Ward ward : wards ) {
        	Long timeleft = 180 - ((now - ward.when) / 1000);
        	if (timeleft > 0) {
        		c.drawCircle(ward.x, ward.y, 4, wardpaint);
        		c.drawText(timeleft.toString(), ward.x, ward.y - 5, timepaint);
        	}
        	else dump.add(ward);        		
        }        
        wards.remove(dump.poll());
        invalidate();
	}
	
	public void setWard(int x, int y) {
		Ward ward = new Ward(x, y);
		wards.add(ward);
		System.out.println("setWard from irc");
	}
	
	public void setWard(Ward w) {		
		wards.add(w);
	}
	
	public void unsetWard(int x1, int y1) {
		for ( Ward ward : wards ) {
			if ( ward.x == x1 && ward.y == y1)
				dump.add(ward);
		}
	}
	
	public void unDo() {
		dump.add(undoable);
		insession.sendMsg("say", "unward " + undoable.x + " " + undoable.y);
	}
	 
	class Ward {
		int x,y;
		Long when = (long) 0;
		 
		public Ward(int x1, int y1) {
			 x = x1; y = y1;
			 when = System.currentTimeMillis();
		}
		 
		@Override
		public String toString() {
		    return "ward" + x + " " + y;
		}
	}
}
